<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Game
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 
 
 
class Game_Form_Admin_Global extends Engine_Form
{
  /*
  public function isValid($data)
  {
    $valid = parent::isValid($data);
    $this->addError('this is stupid');
    return $valid;
  }
  */
  public function init()
  {
    $this
      ->setTitle('Global Settings')
      ->setDescription('These settings affect all members in your community.');
    
    $this->addElement('Text', 'game_license', array(
      'label' => 'Game License Key',
      'description' => 'Enter the your license key that is provided to you when you purchased this plugin. If you do not know your license key, please contact Radcodes support team.',
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.license', 'XXXX-XXXX-XXXX-XXXX'),
      'filters' => array(
        'StringTrim'
      ),
      'allowEmpty' => false,
      'validators' => array(
        new Radcodes_Lib_Validate_License('game'),
      ),
    ));
     
    $user_id = Engine_Api::_()->getApi('settings', 'core')->getSetting('game.userid');
    
    if (empty($user_id)) {
      $viewer = Engine_Api::_()->user()->getViewer();
      $user_id = $viewer->username;
    }
      
    $this->addElement('Text', 'game_userid', array(
      'label' => 'Default Game Owner',
      'description' => 'Enter the member ID (or username, or email) who would be the owner of the imported games. Instead of using your own account, you can create/assign a special member designated to be the owner of these games so that it would not interfere with your real account.',
      'allowEmpty' => false,
      'required' => true,
      'filters' => array(
        'StringTrim',
      ),
      'validators' => array(
        new Engine_Validate_Callback(array($this, 'validateMember')),
      ), 
      'value' => $user_id,
    ));     
    
    $this->addElement('Radio', 'game_preorder', array(
      'label' => 'Pre-Ordering Priority',
      'description' => "You can force sponsored / featured games to display on top of browse page by selecting one of the following",
      'multiOptions' => array(
        0 => "User preference",
        1 => "Sponsored games, then user preference",
        2 => "Sponsored games, featured games, then user preference",
        3 => "Featured games, then user preference",
        4 => "Featured games, sponsored games, then user preference",
      ),
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.preorder', 0),
    ));
    
    $this->addElement('Text', 'game_perpage', array(
      'label' => 'Games Per Page',
      'description' => 'How many games will be shown per page? (Enter a number between 1 and 100)',
      'class' => 'short',
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.perpage', 10),
      'validators' => array(
        'Digits',
        new Zend_Validate_Between(1,100),
      ),
    ));    
    

    
    $this->addElement('Text', 'game_mochipublisherid', array(
      'label' => 'Mochi Publisher ID',
      'description' => "Enter your Mochi's Publisher ID, this field is required to use various of Mochi services such as leaderboard, auto post etc.. You can find the Publisher ID on the Mochi's site under Publisher Tool :: Settings page.",
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.mochipublisherid'),
    ));
    
    $this->addElement('Text', 'game_mochisecretkey', array(
      'label' => 'Mochi Publisher Secret Key',
      'description' => "Enter your Mochi's Publisher Secret Key, this field is required to use various of Mochi services such as leaderboard, auto post etc.. You can find your Secret Key on the Mochi's site under Publisher Tool :: Settings page.",
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.mochisecretkey'),
    ));    
        
    $this->addElement('Radio', 'game_mochibridge', array(
      'label' => 'Mochi Bridge Enabled?',
      'description' => 'Would you like to enable Mochi Bridge to allow communicate between your site and Mochi for games that support leaderboard, coins etc..? If you choose YES, make sure you update your site "crossdomain.xml" file (see FAQ), and provide your Mochi Publisher ID above.',
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.mochibridge', 1),
      'multiOptions' => array(
        '1' => 'Yes, enable Mochi Bridge feature',
        '0' => 'No, disable Mochi Bridge feature',  
      ),
        
      'validators' => array(
        new Engine_Validate_Callback(array($this, 'validateMochiBridge')),
      ),  
            
    )); 

    $this->addElement('Radio', 'game_mochibridgedebug', array(
      'label' => 'Mochi Bridge Debug Enabled?',
      'description' => 'Would you like to enable debug mode for Mochi Bridge? Enable this would display communication info between your site and Mochi for testing and debuging purposes.',
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.mochibridgedebug'),
      'multiOptions' => array(
        '1' => 'Yes, display debug window info log',
        '0' => 'No, hide debug window info log',  
      ),
    )); 

    /*
    $this->addElement('Text', 'game_mochibridgelogo', array(
      'label' => 'Mochi Bridge Site Logo URL',
      'description' => 'Enter a optional full URL (start with http://) to an 16x16 JPG icon for your site. This will display on the leaderboard within the supported games.',
      'value' => Engine_Api::_()->getApi('settings', 'core')->getSetting('game.mochibridgelogo'),
    ));     
      */  
    
    // Add submit button
    $this->addElement('Button', 'submit', array(
      'label' => 'Save Changes',
      'type' => 'submit',
      'ignore' => true
    ));
  }
  
  
  public function validateMember($value)
  {
    $user = Engine_Api::_()->user()->getUser($value);
    if (!$user->getIdentity()) {
      $this->game_userid->getValidator('Engine_Validate_Callback')->setMessage('Could not find user');
      return false;
    }

    return true;
  }   
  
  public function validateMochiBridge($value)
  {
    if ($value && (!$this->game_mochipublisherid->getValue() || !$this->game_mochisecretkey->getValue())) {
      $this->game_mochibridge->getValidator('Engine_Validate_Callback')->setMessage('Mochi Publisher ID and Secret Key are required when you enable Mochi Bridge.');
      return false;
    }
    
    return true;
  } 
  
}